//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.events.BlogLabelEvent;
	import reader.model.IDataProxy;
	import reader.model.vo.TitleWindowVO;
	import reader.view.components.AddBlogLabelPanel;

	public class AddLabelCommand extends SimpleCommand implements ICommand
	{
		//////////////////////////////////////////////////////////////////////////////
		//private variables
		private var dataProxy:IDataProxy;
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function execute(notification:INotification):void
		{
			var labelFetch:TitleWindowVO = notification.getBody() as TitleWindowVO;
			dataProxy = labelFetch.dataProxy;
			
			var panel:AddBlogLabelPanel = AddBlogLabelPanel(PopUpManager.createPopUp(labelFetch.parent, AddBlogLabelPanel, true));
			PopUpManager.centerPopUp(panel);
			panel.blogSourcePointer = labelFetch.blogSource[0];
			panel.addEventListener(BlogLabelEvent.LABEL_CREATE, onLabelCreate_handler);
		}
		
		private function onLabelCreate_handler(e:BlogLabelEvent):void
		{
			var panel:AddBlogLabelPanel = e.currentTarget as AddBlogLabelPanel;
			panel.removeEventListener(BlogLabelEvent.LABEL_CREATE, onLabelCreate_handler);
			dataProxy.addLabel(e.blogLabel, e.blogSource);
		}
	}
}